package com.xique.door.device.hq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.domain.R;
import com.xique.common.core.exception.BaseException;
import com.xique.common.core.utils.DateUtils;
import com.xique.community.api.RemoteCommunityUserService;
import com.xique.community.api.model.CommunityUser;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.AccessDeviceV1;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.bean.domain.Visitor;
import com.xique.door.bean.search.AccessDeviceListSearch;
import com.xique.door.bean.search.UserDeviceListSearch;
import com.xique.door.device.hq.bean.constant.HqDeviceConstant;
import com.xique.door.device.hq.bean.dto.HqDeviceDownPersonDTO;
import com.xique.door.device.hq.bean.request.HqDeviceDelPersonRequest;
import com.xique.door.device.hq.bean.request.HqDeviceDownPersonRequest;
import com.xique.door.device.hq.bean.request.HqDeviceDownPersonsRequest;
import com.xique.door.device.hq.bean.request.HqDeviceGetStatusRequest;
import com.xique.door.device.hq.bean.response.*;
import com.xique.door.device.hq.service.IHqDeviceCommandBizService;
import com.xique.door.device.hq.service.IHqDeviceCommandService;
import com.xique.door.service.IAccessDeviceService;
import com.xique.door.service.IUserDeviceService;
import com.xique.door.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2021/9/18 16:08
 */
@Service
public class HqDeviceCommandBizServiceImpl implements IHqDeviceCommandBizService {

    @Autowired
    private IHqDeviceCommandService hqDeviceCommandService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Autowired
    private IAccessDeviceService accessDeviceService;

    @Autowired
    private RemoteCommunityUserService remoteCommunityUserService;

    @Autowired
    private IVisitorService visitorService;

    @Override
    public HqDownPersonCommandResponse downPerson(Long userId, List<String> deviceSns) {

        R<CommunityUser> userResult = remoteCommunityUserService.getUserInfo(userId);
        if (R.FAIL == userResult.getCode()) {
            throw new BaseException(userResult.getMsg());
        }
        CommunityUser user = userResult.getData();

        HqDeviceDownPersonDTO info = new HqDeviceDownPersonDTO();
        info.setCustomId(user.getCustomId());
        info.setName(user.getUserName());
        info.setPersonType(user.getPersonType());
        info.setTempCardType(Integer.parseInt(user.getVisitorType()));
        info.setPicURI(user.getFaceUrl());
        HqDeviceDownPersonRequest request = new HqDeviceDownPersonRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(deviceSns);
        request.setInfo(info);
        JSONObject object = hqDeviceCommandService.downPersonCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqDownPersonCommandResponse.class);
    }

    @Override
    public HqDownPersonsCommandResponse downPersons(List<Long> userIds, List<String> deviceSns) {

        R<List<CommunityUser>> listResult = remoteCommunityUserService.getUserList(userIds);
        if (R.FAIL == listResult.getCode()) {
            throw new BaseException(listResult.getMsg());
        }
        List<HqDeviceDownPersonDTO> info = new ArrayList<>();
        List<CommunityUser> list = listResult.getData();
        list.forEach(item -> {
            HqDeviceDownPersonDTO dto = new HqDeviceDownPersonDTO();
            dto.setCustomId(item.getCustomId());
            dto.setName(item.getUserName());
            dto.setPersonType(item.getPersonType());
            dto.setTempCardType(Integer.parseInt(item.getVisitorType()));
            dto.setPicURI(item.getFaceUrl());
            info.add(dto);
        });

        HqDeviceDownPersonsRequest request = new HqDeviceDownPersonsRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(deviceSns);
        request.setInfo(info);
        JSONObject object = hqDeviceCommandService.downPersonsCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqDownPersonsCommandResponse.class);
    }

    @Override
    public HqDelPersonCommandResponse delPerson(Long userId, List<Long> deviceIds) {

        R<CommunityUser> userResult = remoteCommunityUserService.getUserInfo(userId);
        if (R.FAIL == userResult.getCode()) {
            throw new BaseException(userResult.getMsg());
        }
        CommunityUser user = userResult.getData();

        UserDeviceListSearch userDeviceListSearch = new UserDeviceListSearch();
        userDeviceListSearch.setUserId(userId);
        userDeviceListSearch.setDeviceIds(deviceIds);
        List<UserDevice> userDeviceList = userDeviceService.selectUserDeviceList(userDeviceListSearch);
        List<Long> notDelDeviceIds = userDeviceList.stream().map(UserDevice::getDeviceId).distinct().collect(Collectors.toList());
        deviceIds.removeAll(notDelDeviceIds);
        AccessDeviceListSearch accessDeviceListSearch = new AccessDeviceListSearch();
        accessDeviceListSearch.setDeviceIds(deviceIds);
        List<AccessDeviceV1> accessDeviceList = accessDeviceService.selectAccessDeviceList(accessDeviceListSearch);
        List<String> devSns = accessDeviceList.stream().map(AccessDeviceV1::getDevSn).collect(Collectors.toList());

        HqDeviceDelPersonRequest request = new HqDeviceDelPersonRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(devSns);
        request.setInfo(Collections.singletonMap("customId", user.getCustomId()));
        JSONObject object = hqDeviceCommandService.delPersonCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqDelPersonCommandResponse.class);
    }

    @Override
    public HqDeviceCommandResponse delPersons(List<Long> userIds, List<String> deviceSns) {
        return null;
    }

    @Override
    public HqGetStatusCommandResponse getStatus(List<Long> userIds, List<String> deviceSns) {

        R<List<CommunityUser>> listResult = remoteCommunityUserService.getUserList(userIds);
        if (R.FAIL == listResult.getCode()) {
            throw new BaseException(listResult.getMsg());
        }
        List<CommunityUser> list = listResult.getData();
        List<String> customIds = list.stream().map(CommunityUser::getCustomId).collect(Collectors.toList());

        HqDeviceGetStatusRequest request = new HqDeviceGetStatusRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(deviceSns);
        request.setClassName("User");
        request.setCustomIds(customIds);

        JSONObject object = hqDeviceCommandService.getStatusCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqGetStatusCommandResponse.class);
    }

    @Override
    public HqDownPersonCommandResponse downVisitor(Long visitorId, List<String> deviceSns) {

        Visitor visitor = visitorService.selectVisitorById(visitorId);

        HqDeviceDownPersonDTO info = new HqDeviceDownPersonDTO();
        info.setCustomId(visitor.getCustomId());
        info.setName(visitor.getVisitorName());
        info.setPersonType(visitor.getPersonType());
        info.setTempCardType(Integer.parseInt(visitor.getVisitorType()));
        info.setPicURI(visitor.getFaceUrl());
        info.setCardValidBegin(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, visitor.getStartTime()));
        info.setCardValidEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, visitor.getEndTime()));
        info.setEffectNumber(visitor.getEffectNumber());
        HqDeviceDownPersonRequest request = new HqDeviceDownPersonRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(deviceSns);
        request.setInfo(info);
        JSONObject object = hqDeviceCommandService.downPersonCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqDownPersonCommandResponse.class);
    }

    @Override
    public HqDelPersonCommandResponse delVisitor(Long visitorId, List<String> deviceSns) {

        Visitor visitor = visitorService.selectVisitorById(visitorId);

        HqDeviceDelPersonRequest request = new HqDeviceDelPersonRequest();
        request.setVersion(HqDeviceConstant.Version.VERSION_1);
        request.setDeviceSns(deviceSns);
        request.setInfo(Collections.singletonMap("customId", visitor.getCustomId()));
        JSONObject object = hqDeviceCommandService.delPersonCommandIssue(request);

        return JSONObject.parseObject(String.valueOf(object), HqDelPersonCommandResponse.class);
    }
}
